package cn.jekun.shop.web.action;


import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.web.bean.MemberSession;
import cn.jekun.shop.web.service.LoginService;

/**
 * 登录
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class LoginAction extends ShopBaseAction{
	@Resource(name = LoginService.BEAN_NAME)
	LoginService loginService;

	public String login(){
//		System.out.println(System.getProperty("file.encoding"));
		JSONObject jsonObject = null;
		String userName = this.request.getParameter("userName");
		String password = this.request.getParameter("password");//MD5
		try{
			jsonObject = loginService.login(userName,password);
			MemberSession memberSession = new MemberSession();
			memberSession.setUserId(jsonObject.get("userId").toString());
			memberSession.setUserName(jsonObject.get("userName").toString());
			memberSession.setMobile(jsonObject.getString("mobile").toString());
			memberSession.setIconUrl(jsonObject.getString("iconUrl"));
			memberSession.setShopId(jsonObject.getString("shopId"));
			memberSession.setShopName(jsonObject.getString("shopName"));
			memberSession.setShopPrintType(jsonObject.getString("printType"));
			memberSession.setAgentId(jsonObject.getString("agentId"));
			memberSession.setAgentName(jsonObject.getString("agentName"));
			memberSession.setAgentMobile(jsonObject.getString("agentMobile"));
			memberSession.setBrowser(getBrowser());
			memberSession.setIp(getIp());
			memberSession.setUserName(userName);
			memberSession.setPwd(password);
			this.request.getSession().setAttribute(MemberSession.SESSION_KEY, memberSession);
	
			String agent = request.getHeader("USER-AGENT");//
			String memo = "会员在web登录."+agent;
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), memo.length()>400?memo.substring(0, 400):memo);//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			try {
				this.logService.addMallLog(userName, 
						getBrowser(), getIp(), "用户登录失败,原因:"+e.getMessage()+",来源:web,用户代码:"+userName);//记录操作日志
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
	
		}
		return this.writeJson(jsonObject);
	}


	/**
	 * 退出系统
	 * @return
	 */
	public String logout(){
		JSONObject jsonObject = null;
		jsonObject = loginService.logout();
		request.getSession().removeAttribute(MemberSession.SESSION_KEY);
		request.getSession().invalidate();
		return this.writeJson(jsonObject);
	}

	/**
	 * 找回登录密码
	 * 发送手机验证码
	 * @return
	 */
	public String sendValidateCode(){

		JSONObject jsonObject = null;

		try {
			jsonObject = loginService.sendValidateCode(request.getParameter("mobile"));
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 校验验证码是否正确
	 * @return
	 */
	public String  getValidateCode(){
	  
		JSONObject jsonObject = null;

		try {
			jsonObject = loginService.checkValidateCode(request.getParameter("mobile")
					,request.getParameter("validatecode"));
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
	}
	
	/**
	 * 重置登录密码
	 * @return
	 */
	public String reLoginPwd(){
		
		JSONObject jsonObject = null;

		try {
			jsonObject = loginService.reLoginPwd(request.getParameter("password")
					,request.getParameter("mobile"));
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "用户重置登录密码成功,来源:web,用户代码:"+this.getUserId());//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "用户重置登录密码失败,原因:"+jsonObject.get("message")+",来源:web,用户代码:"+this.getUserId());//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
	}
}
